/**
 *
 * @author John Creecy
 */
package server;

import server.entities.PlayerActionEntity;

public class ProcessorThread implements Runnable {

	private String packetData;
	private String sourceIp;
	private String message;
	private int movementDirection; // 0 none, 1 up, 2 right, 3 down, 4 left
	private int movementUnits;
	private int x;
	private int y;
	private int sourcePort;

	public ProcessorThread(String packetDataString, String ipAddress, int port) {
		x = 0;
		y = 0;
		this.packetData = packetDataString;
		this.sourceIp = ipAddress;
		this.sourcePort = port;
		movementUnits = 10;
	}

	public void run() {
		processPacketData();
	}

	private void processPacketData() {
		if(BattleBarn.actionStack.containsKey(sourceIp)) {
			PlayerActionEntity previousActionEntity = BattleBarn.actionStack.get(sourceIp);
			x = previousActionEntity.getX();
			y = previousActionEntity.getY();
		}
		movementDirection = Integer.parseInt(this.packetData.substring(0,1));
	
		// 0 none, 1 up, 2 right, 3 down, 4 left
		switch(movementDirection) {
			case 1: y=(y-movementUnits); break;
			case 2: x=(x+movementUnits); break;
			case 3: y=(y+movementUnits); break;
			case 4: x=(x-movementUnits); break;
			default: break;
		}

		PlayerActionEntity playerActionEntity = new PlayerActionEntity(sourceIp, Math.abs(x), Math.abs(y), message, movementUnits);
		BattleBarn.actionStack.put(sourceIp, playerActionEntity);
	}
}
